package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.conn.GetCodePost;
import com.longcai.piaojia.conn.UpdatePhonePost;
import com.longcai.piaojia.fragment.MineFragment;
import com.longcai.piaojia.util.MyToast;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.util.UtilMatches;

/**
 * Created by Administrator on 2016/8/22.
 */
public class EditPhoneNumActivity extends BaseActivity implements View.OnClickListener{

    @BoundView(R.id.edit_phonenum_phone_et) private EditText edit_phonenum_phone_et;
    @BoundView(R.id.edit_phonenum_verification_code_et) private EditText edit_phonenum_verification_code_et;
    @BoundView(R.id.edit_phonenum_submit_bt) private TextView edit_phonenum_submit_bt;
    @BoundView(R.id.edit_phonenum_verification_code_tv) private TextView edit_phonenum_verification_code_tv;

    private GetCodePost.CodeEntity codeEntityEditPhoneNum;

    private int netCode;

    private String change_token;

    private CountDownTimer countTime = new CountDownTimer(60000, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {

            if (!isFinishing()) {

                edit_phonenum_verification_code_tv.setText(millisUntilFinished / 1000
                        + getString(R.string.edit_phonenum_code_count_down_time_text));

            }
        }

        @Override
        public void onFinish() {

            if (!isFinishing()) {

                edit_phonenum_verification_code_tv.setText(getString(R.string.edit_phonenum_code_text));
                edit_phonenum_verification_code_tv.setEnabled(true);
            }
        }
    };

    private GetCodePost getCodePost = new GetCodePost(new AsyCallBack<GetCodePost.CodeEntity>(){

        @Override
        public void onSuccess(String toast, int type, GetCodePost.CodeEntity codeEntity) throws Exception {
            super.onSuccess(toast, type, codeEntity);

            codeEntityEditPhoneNum = codeEntity;

            netCode = codeEntity.code;

            Toast.makeText(EditPhoneNumActivity.this, toast, Toast.LENGTH_LONG).show();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(EditPhoneNumActivity.this, toast, Toast.LENGTH_LONG).show();

            countTime.cancel();
            edit_phonenum_verification_code_tv.setEnabled(true);
            edit_phonenum_verification_code_tv.setText(getString(R.string.register_SMS_verification_code_text));

        }
    });

    private UpdatePhonePost UpdatePhonePost = new UpdatePhonePost(new AsyCallBack<String>(){

        @Override
        public void onSuccess(String toast, int type, String s) throws Exception {
            super.onSuccess(toast, type, s);

            Toast.makeText(EditPhoneNumActivity.this, toast, Toast.LENGTH_LONG).show();

            BaseApplication.BasePreferences.saveIsLogin(false);
            BaseApplication.BasePreferences.saveUserId("");
            BaseApplication.BasePreferences.saveUserAccount("");

            startActivity(new Intent(EditPhoneNumActivity.this, LoginActivity.class));

            BaseApplication.INSTANCE.finishActivity(MyAccountManagerActivity.class, ConfirmOldPhoneActivity.class, EditPhoneNumActivity.class);

//            MineFragment.getUserAdvert getUserAdvert = (MineFragment.getUserAdvert) getAppCallBack(MineFragment.class);
//            getUserAdvert.updatePhone(edit_phonenum_phone_et.getText().toString().trim());
            MineFragment.getUserAdvert getUserAdvert = (MineFragment.getUserAdvert) getAppCallBack(MineFragment.class);

            getUserAdvert.refreshInfo();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {
            super.onFail(toast, type);

            Toast.makeText(EditPhoneNumActivity.this, toast, Toast.LENGTH_LONG).show();

        }

    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_edit_phonenum);

        setBackTrue();

        setTitleName(getString(R.string.edit_phonenum_title_text));

        edit_phonenum_verification_code_tv.setOnClickListener(this);
        edit_phonenum_submit_bt.setOnClickListener(this);

        change_token = getIntent().getStringExtra("change_token");
    }

    @Override
    public void onClick(View v) {

        String phone = edit_phonenum_phone_et.getText().toString().trim();
        String code = edit_phonenum_verification_code_et.getText().toString().trim();

        switch (v.getId()) {

            case R.id.edit_phonenum_verification_code_tv:

                if(TextUtils.isEmpty(phone)){

                    MyToast.show(context, getString(R.string.warning_please_fill_phone_text), Toast.LENGTH_LONG);

                    return;

                }else if (!UtilMatches.checkMobile(phone)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_true_phone_text), Toast.LENGTH_LONG);

                    return;
                }

                getCodePost.phone = phone;
                getCodePost.type = "reg";
                getCodePost.change = "true";
                getCodePost.execute(context);

                edit_phonenum_verification_code_tv.setEnabled(false);

                countTime.start();

                break;

            case R.id.edit_phonenum_submit_bt:

                if (TextUtils.isEmpty(phone)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_phone_text), Toast.LENGTH_LONG);

                    return;

                } else if (!UtilMatches.checkMobile(phone)) {

                    MyToast.show(context, getString(R.string.warning_please_fill_true_phone_text), Toast.LENGTH_LONG);

                    return;
                } else if(netCode == 0){

                    MyToast.show(context, getString(R.string.warning_please_click_get_code_text), Toast.LENGTH_LONG);

                    return;

                }else if(TextUtils.isEmpty(code)){

                    MyToast.show(context, getString(R.string.register_verification_code_hint), Toast.LENGTH_LONG);

                    return;

                }else if(Integer.parseInt(code)!=codeEntityEditPhoneNum.code){

                    MyToast.show(context, getString(R.string.warning_bad_code_text), Toast.LENGTH_LONG);

                    return;

                }

                UpdatePhonePost.phone = phone;
                UpdatePhonePost.code = code;
                UpdatePhonePost.user_id = BaseApplication.BasePreferences.readUserId();
                UpdatePhonePost.change_token = change_token;
                UpdatePhonePost.execute(EditPhoneNumActivity.this);

                break;
        }

    }


}
